package com.light.gulimall.cart.vo;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车的整体信息
 */
public class Cart {

    /**
     * 单个购物车项目组成的集合
     */
    private List<CartItem> items;

    /**
     * 商品总数
     */
    private Integer countNum;

    /**
     * 商品类型数量
     */
    private Integer countType;

    /**
     * 商品总价
     */
    private BigDecimal totalAmount;

    /**
     * 减免价格
     */
    private BigDecimal reduce = new BigDecimal("0.00");


    public Cart() {
    }

    public Cart(List<CartItem> items, Integer countNum, Integer countType, BigDecimal totalAmount, BigDecimal reduce) {
        this.items = items;
        this.countNum = countNum;
        this.countType = countType;
        this.totalAmount = totalAmount;
        this.reduce = reduce;
    }

    /**
     * 获取
     * @return items
     */
    public List<CartItem> getItems() {
        return items;
    }

    /**
     * 设置
     * @param items
     */
    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    /**
     * 获取
     * @return countNum
     */
    public Integer getCountNum() {
        int count = 0;
        if (!CollectionUtils.isEmpty(this.items)) {
            for (CartItem item : this.items) {
                count += item.getCount();
            }
        }
        return count;
    }

    /**
     * 获取
     * @return countType
     */
    public Integer getCountType() {
        return this.items.size();
    }

    /**
     * 获取
     * @return totalAmount
     */
    public BigDecimal getTotalAmount() {
        BigDecimal total = new BigDecimal("0.00");
        if (!CollectionUtils.isEmpty(this.items)) {
            for (CartItem item : this.items) {
                if (item.getCheck()){
                    total = total.add(item.getTotalPrice());
                }
            }
        }

        total = total.subtract(getReduce());
        return total;
    }


    /**
     * 获取
     * @return reduce
     */
    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }

    @Override
    public String toString() {
        return "Cart{items = " + items + ", countNum = " + countNum + ", countType = " + countType + ", totalAmount = " + totalAmount + ", reduce = " + reduce + "}";
    }
}
